<template>
	<view>
		<view class="r_balance">
			<view class="r_b_number">￥{{myBalance}}</view>
			<view class="r_b_title">账户余额</view>
		</view>
		<view class="r_number_list">
			<view class="r_number_item_tr">
				<view class="r_number_item" @click="select(item.internal_id, item.price)" :class="item.internal_id==balance?'r_avatr':''"
				 v-for="(item, index) in list.slice(0, 3)">{{item.price}}<text>元</text></view>
			</view>
			<view class="r_number_item_tr">
				<view class="r_number_item" @click="select(item.internal_id, item.price)" :class="item.internal_id==balance?'r_avatr':''"
				 v-for="(item, index) in list.slice(3)">{{item.price}}<text>元</text></view>
				<view class="r_number_item_no"></view>
			</view>
			<!-- 			<view class="r_rechargeInput">
				<input type="number" placeholder="请选择金额" disabled v-model="balance" />
			</view> -->
		</view>
		<view class="r_type">
			<view class="r_img">
				<image src="../../../../static/my/myBack/apple_Pay.png"></image>
			</view>
			<view class="r_text">
				<view class="r_t_top">苹果支付</view>
				<view class="r_t_bottom">推荐苹果用户使用</view>
			</view>
			<view class="r_icon">
				<image src="../../../../static/my/myBack/gou0.png" mode=""></image>
			</view>
		</view>
		<view class="r_bottom">
			<view class="r_button" @click="confirmFun">确定充值</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				myBalance: 0,
				balance: 0,
				payPrice: 0,
				iapChannel: null,
				list: []
			}
		},
		onLoad() {
			const that = this;
			plus.payment.getChannels(function(channels) {
				for (var i in channels) {
					if (channels[i].id == 'appleiap') {
						that.iapChannel = channels[i];
						// iapChannel.requestOrder(IAPOrders, function(event) {

						// }, function(errormsg) {
						// 	uni.showToast({
						// 		icon: 'none',
						// 		title: "获取支付通道失败：" + errormsg.message
						// 	})
						// });
					}
				}
			}, function(e) {
				uni.showToast({
					icon: 'none',
					title: "获取支付通道失败：" + e.message
				})
			});
		},
		onShow() {
			this.getList();
			this.getMyBalance();
		},
		methods: {
			getMyBalance() {
				this.$utils.request('/api/user_wallet', '', res => {
					if (res.code == 200) {
						this.myBalance = res.data
					} else {
						uni.showToast({
							icon: 'none',
							title: '请检查网络'
						})
					}
				})
			},
			select(balance, payPrice) {
				this.balance = balance;
				this.payPrice = payPrice;
			},
			getList() {
				const that = this;
				this.$utils.postrequest("/api/recharge/list", {}, res => {
					if (res.code == 200) {
						that.list = res.data;
						that.balance = that.list[0]["internal_id"];
						that.payPrice = that.list[0]["price"];
					}
				})
			},
			confirmFun() {
				// console.log("进行充值")
				const that = this;
				uni.requestPayment({
					provider: "appleiap",
					orderInfo: {
						productid: this.balance,
					},
					success: (result) => {
						that.$utils.postrequest("/api/ios/order", {
							"pay_price": that.payPrice,
							"ios_order_json": result
						}, res => {
							if (res.code == 200) {
								that.getMyBalance();
								uni.showToast({
									icon: 'success',
									title: '充值成功'
								})
							} else {
								uni.showToast({
									icon: 'none',
									title: res.message
								})
							}
						})
					},
					fail: (e) => {
						uni.showToast({
							title: '支付失败'
						})
					},
					complete: () => {
						// console.log("payment结束");
					},
				});
				// plus.payment.request(that.iapChannel, {
				// 	"productid": that.balance,
				// }, function(result) {
				// 	// console.log(result)
				// 	that.$utils.postrequest("/api/ios/order", {
				// 		"pay_price": that.payPrice,
				// 		"ios_order_json": result
				// 	}, res => {
				// 		if (res.code == 200) {
				// 			that.getMyBalance();
				// 			uni.showToast({
				// 				icon: 'success',
				// 				title: '充值成功'
				// 			})
				// 		} else {
				// 			uni.showToast({
				// 				icon: 'none',
				// 				title: res.message
				// 			})
				// 		}
				// 	})
				// }, function(e) {
				// 	// console.log(
				// 		"更多错误信息请参考支付(Payment)规范文档：http://www.html5plus.org/#specification#/specification/Payment.html", null, "支付失败：" +
				// 		e.code);
				// });
			}
		},
	}
</script>
<style scoped>
	.r_balance {
		width: 100%;
		height: 500rpx;
		background-image: url(../../../../static/my/myBack/rechargeBalance_bg.png);
		background-repeat: no-repeat;
		background-size: 100% 100%;
		border: 1px solid;
	}

	.r_b_number {
		margin-top: 90rpx;
		width: 100%;
		height: 45rpx;
		line-height: 45rpx;
		text-align: center;
		font-size: 36rpx;
		font-weight: 800;
		color: #FFFFFF;
	}

	.r_b_title {
		margin-top: 32rpx;
		width: 100%;
		line-height: 36rpx;
		text-align: center;
		height: 36rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #FFFFFF;
	}

	.r_number_list {
		width: 690rpx;
		margin: 0 auto;
		position: relative;
		top: -136rpx;
		z-index: 1;
		padding: 28rpx 23rpx;
		box-sizing: border-box;
		box-shadow: 0px 0px 16px 0px rgba(0, 0, 0, 0.09);
		border-radius: 30px;
		overflow: hidden;
		background-color: #FFFFFF;
	}

	.r_number_item_tr {
		width: 100%;
		height: 129rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 14rpx;
	}

	.r_number_item {
		width: 202rpx;
		height: 129rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border: 2rpx solid #6d50ff;
		border-radius: 10px;
		font-size: 40rpx;
		font-weight: 800;
		color: #6d50ff;
	}

	.r_number_item_no {
		width: 202rpx;
		height: 129rpx;
	}

	.r_number_item text {
		font-size: 26rpx;
		font-weight: 500;
		margin-left: 5rpx;
	}

	.r_avatr {
		color: #FFFFFF !important;
		background-color: #6d50ff !important;
	}

	.r_rechargeInput {
		margin-top: 42rpx;
		width: 100%;
		height: 100rpx;
		background: #F7F4F8;
		border-radius: 10px;
		overflow: hidden;
		display: flex;
		align-items: center;
		padding-left: 10rpx;
		box-sizing: border-box;
	}

	.r_type {
		width: 700rpx;
		height: 106rpx;
		position: relative;
		top: -136rpx;
		border-bottom: 1rpx solid #E6E6E6;
		padding-bottom: 29rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		margin: auto;
		margin-top: 37rpx;
	}

	.r_img {
		width: 77rpx;
		height: 77rpx;
		margin-right: 12rpx;
	}

	.r_img image {
		width: 100%;
		height: 100%;
	}

	.r_text {
		width: auto;
		height: 100%;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.r_t_top {
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
	}

	.r_t_bottom {
		font-size: 24rpx;
		font-weight: 500;
		color: #949492;
	}

	.r_icon {
		width: 38rpx;
		height: 38rpx;
		margin-left: auto;
	}

	.r_icon image {
		width: 100%;
		height: 100%;
	}

	.r_bottom {
		width: 100%;
		height: 114rpx;
		position: fixed;
		left: 0;
		bottom: 113rpx;
	}

	.r_button {
		margin: auto;
		width: 691rpx;
		height: 114rpx;
		background: linear-gradient(0deg, #6d50ff, #6d50ff);
		border-radius: 57rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 36rpx;
		font-weight: bold;
		color: #FFFFFF;
	}
</style>
